package gui;

import gui.AddRemoveTab.addSuppliers;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import users.Manager;

public class RemoveItem extends JPanel{

	private static final long serialVersionUID = -7169583569695472633L;
	
	private Manager manager;
	//private JFrame main;
	private JTextField upcText;
	private JButton reItem;
	private JTextArea textArea;
	private JScrollPane confirmation;
	
	public RemoveItem(Manager manager){
		this.manager = manager;
		
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		removePanel();
		confirmPanel();
		
	}
	
	private void removePanel(){
		JPanel remove = new JPanel();
		remove.setLayout(new GridLayout(2, 2));
		remove.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		
		remove.add(new JLabel("UPC of Item to be removed: "));
        upcText = new JTextField();
        upcText.setPreferredSize(new Dimension(300, 30));
        remove.add(upcText);
        
		remove.add(new JLabel("Click to remove item"));
		reItem = new JButton("remove Item");
		reItem.addActionListener(new removeItem());
		remove.add(reItem);
		
		add(remove);
	}
	
	private void confirmPanel(){
		JPanel conPanel = new JPanel(new BorderLayout());
		TitledBorder title;
		title = BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Remove Confirmation");
		conPanel.setBorder(title);
		
        textArea = new JTextArea();
        textArea.setEditable(false);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        
        confirmation = new JScrollPane(textArea);
        confirmation.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        
        conPanel.add(confirmation, BorderLayout.CENTER);
		
        add(conPanel);
	}
	
	public class removeItem implements ActionListener{
		String upc;
		public void actionPerformed(ActionEvent arg0) {
			upc=upcText.getText();
			try {
				manager.removeItem(upc);
				textArea.append("******************* Item Deleted ******************" + "\n" +
						"\n" + "Supplier with upc: " + upc + " has been removed successfully" +
						"\n\n" +
						"*********************************************************" + "\n\n");
			} catch (SQLException e) {
				textArea.append("Item does not exist.  Cannot be deleted!");
				e.printStackTrace();
			}
			
		}
		
	}
}
